package com.woniuxy.lrz.carlproject.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.Log;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.common.util.BeanCopyUtil;
import com.woniuxy.lrz.carlproject.common.util.OssTemplate;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.serviceform.AddServiceForm;
import com.woniuxy.lrz.carlproject.controller.form.serviceform.UpdateServiceForm;
import com.woniuxy.lrz.carlproject.model.ServiceFirstLevel;
import com.woniuxy.lrz.carlproject.model.ServiceInfo;
import com.woniuxy.lrz.carlproject.model.ServiceSecondLevel;
import com.woniuxy.lrz.carlproject.service.ServiceSecondLevelService;
import com.woniuxy.lrz.carlproject.service.dto.ServiceDto.ServiceInfoDto;
import com.woniuxy.lrz.carlproject.service.ServiceInfoService;
import com.woniuxy.lrz.carlproject.service.dto.ServiceDto.ServiceInfoFindDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 服务表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@RequestMapping("/carlproject/service")
@Api("服务接口")
@Slf4j
public class ServiceInfoController {

    @Resource
    private ServiceInfoService serviceInfoService;
    @Resource
    private ServiceSecondLevelService serviceSecondLevelService;
    @GetMapping("/getServiceById")
    @Log("根据服务id查询服务详情")
    @ApiOperation("根据服务id查询服务详情")
    @ApiImplicitParam("服务id")
    public Result getServiceById( Integer serviceId){
        log.info("服务id：",serviceId);
        if (serviceId == null || serviceId<1){
            throw  new AppException("参数错误");
        }
        ServiceInfo serviceById = serviceInfoService.getServiceById(serviceId);
        ServiceInfoDto serviceInfoDto = BeanUtil.copyProperties(serviceById, ServiceInfoDto.class);
        return Result.success(serviceInfoDto);
    }


    @GetMapping("/getAll")
    @Log("查询所有服务详情")
    @ApiOperation("查询服务详情")
    public Result getAll(@RequestParam(defaultValue = "1")int current, @RequestParam(defaultValue = "10")  int size){
        Page<ServiceInfo> all = serviceInfoService.getAll(current, size);
        Page<ServiceInfoFindDto> serviceInfoFindDtoPage = BeanCopyUtil.copyPageInfo(all, ServiceInfoFindDto.class);
        for (ServiceInfoFindDto record : serviceInfoFindDtoPage.getRecords()) {
            ServiceSecondLevel service_second_id = serviceSecondLevelService.getOne(new QueryWrapper<ServiceSecondLevel>().eq("service_second_id", record.getServiceSecondId()));
            Integer serviceFirstId = service_second_id.getServiceFirstId();
            record.setServiceFirstId(serviceFirstId);
        }
        return Result.success(serviceInfoFindDtoPage);
    }

    @PostMapping("/addService")
    @Log("添加服务")
    @ApiOperation("添加服务")
    @ApiImplicitParam("添加服务对象")
    public Result addService(@Valid @RequestBody AddServiceForm addServiceForm){
        log.info("服务表单:{}",addServiceForm);
        serviceInfoService.add(addServiceForm);
        return Result.success();
    }

    @PutMapping("/updateService")
    @Log("修改服务详情")
    @ApiOperation("修改服务详情")
    @ApiImplicitParam("修改服务表单")
    public  Result updateService(@Valid @RequestBody UpdateServiceForm updateServiceForm){
       log.info("修改服务表单:{}",updateServiceForm);
       serviceInfoService.updateService(updateServiceForm);
       return Result.success();
    }

    @DeleteMapping("/delete")
    @Log("逻辑删除服务")
    @ApiOperation("逻辑删除服务")
    @ApiImplicitParam("服务id")
    public Result deleteById(Integer serviceId){
        log.info("服务id:{}",serviceId);
        if (serviceId<1 || serviceId==null){
            throw  new AppException("参数错误");
        }
        serviceInfoService.deleteById(serviceId);
        return  Result.success();
    }

    @GetMapping("/findServiceSecondList")
    @Cacheable(value = "service",key = "'serviceFirst:'+#serviceFirstId")
    @ApiOperation("加载二级服务列表")
    @ApiImplicitParam(name = "serviceFirstId",value = "一级服务类目ID")
    public Results findServiceSecondList(@NotNull(message = "一级服务类目Id不能为空") Integer serviceFirstId){
        List<ServiceSecondLevel> service_first_id = serviceSecondLevelService.list(new QueryWrapper<ServiceSecondLevel>().eq("service_first_id", serviceFirstId));
        return Results.instance().setData(service_first_id).setMessage("加载二级服务列表成功");
    }

    @PostMapping("/servicePhoto")
    @ApiOperation("上传商品图片")
    public Results servicePhoto(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String s = OssTemplate.fileUpload("img/"+originalFilename,inputStream, "woniuxy-81-3");
        log.info("图片URL:{}",s);
        return Results.instance().setData(s);
    }

    //根据二级服务id查询所有服务详情
    @GetMapping("/getServiceInfoBy2Id")
    @Log("根据二级服务id查询所有服务详情")
    @ApiOperation("根据二级服务id查询所有服务详情")
    public Result getServiceInfoBy2Id(Integer serviceSecondId){
        List<ServiceInfo> srviceInfo = serviceInfoService.getServiceInfoBy2Id(serviceSecondId);
        return Result.success(srviceInfo);
    }
}
